package com.tuanbiao.keyevent;

import android.text.TextUtils;

import com.abiao.lib_utils.StringUtils;
import com.tuanbiao.keyevent.listener.OnBlastKeyDownListener;
import com.tuanbiao.keyevent.listener.OnBlastNettedKeyDownListener;
import com.tuanbiao.keyevent.listener.OnKeyDownListener;
import com.tuanbiao.keyevent.listener.OnLoginKeyDownListener;
import com.tuanbiao.keyevent.listener.OnSettingKeyDownListener;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;

/**
 * @author abiao
 * @date 2022/6/6 15:20
 * @description:
 */
public class AlpsKeyEvent implements KeyEventStrategy {
    public static final int KEY_SCAN_LEFT = 134;
    public static final int KEY_SCAN_RIGHT = 135;
    public static final int KEY_BLAST_LEFT = 137;
    public static final int KEY_BLAST_RIGHT = 138;
    public static final int KEY_MORE = 136;
    public static final int KEY_ENTER = 66;
    public static final int KEY_PLUS = 131;
    public static final int KEY_SUBTRACT = 133;
    public static final int KEY_EDIT = 132;
    public static final int KEY_0 = 7;
    public static final int KEY_1 = 8;
    public static final int KEY_2 = 9;
    public static final int KEY_3 = 10;
    public static final int KEY_4 = 11;
    public static final int KEY_5 = 12;
    public static final int KEY_6 = 13;
    public static final int KEY_7 = 14;
    public static final int KEY_8 = 15;
    public static final int KEY_9 = 16;
    public static final int KEY_F1 = 139;
    public static final int KEY_F2 = 140;
    private int pressCount = 0;
    private StringBuilder keyDownBuf = new StringBuilder();
    private String mInputPsdStr;


    @Override
    public String getNettedNoteSuccess() {
        return "提示:打开安全锁，按F2进入充电";
    }

    @Override
    public String getNettedNoteFailed() {
        return "校验失败，按F1查看错误消息";
    }

    @Override
    public String getChargeNoteSuccess() {
        return "充电完成，可以起爆！请再次确定人员安全撤离！按左右起爆键起爆";
    }

    @Override
    public String getChargeNoteFailed() {
        return "按F2键查看异常雷管信息";
    }

    @Override
    public String getChargeNoteFailed(String msg) {
        return msg + ",按F2键查看充电异常雷管信息";
    }

    @Override
    public String getBlastNoteSuccess() {
        return "爆破完成，可以查看爆破记录！";
    }

    @Override
    public String getBlastNoteFailed() {
        return "起爆异常！请再次确定人员安全撤离！按【F2】查看异常信息";
    }

    @Override
    public String getBlastNoteFailed(String msg) {
        return msg + ",按【F2】查看异常信息";
    }

    @Override
    public List<Integer> getScanModeKeys() {
        List<Integer> keys = new ArrayList<>();
        keys.add(KEY_F2);
        return keys;
    }

    @Override
    public void checkLoginKeyDown(int keyCode, OnLoginKeyDownListener listener) {
        if (keyCode == KEY_F1) {
            Timber.i("checkLoginKeyDown" + pressCount);
            pressCount++;
            if (pressCount >= 4) {
                listener.onChangeMacIMEI();
            }
            postDelayed(() -> {
                pressCount = 0;
            }, CLICK_INTERVAL);
        } else if (keyCode == KEY_F2) {
            Timber.i("checkLoginKeyDown" + pressCount);
            pressCount++;
            if (pressCount >= 4) {
                listener.onChangeDeviceNum();
            }
            postDelayed(() -> {
                pressCount = 0;
            }, CLICK_INTERVAL);
        }
    }

    @Override
    public void checkBlastKeyDown(int keyCode, OnBlastKeyDownListener listener) {
        if (keyCode == KEY_BLAST_LEFT) {
            keyDownBuf.append("F1");
            checkBlastKeyEvent(listener);
        }
        if (keyCode == KEY_BLAST_RIGHT) {
            keyDownBuf.append("F4");
            checkBlastKeyEvent(listener);
        }
        if (keyCode == KEY_F2) {
            listener.onCheckErrorCaps();
        }
    }

    private void checkBlastKeyEvent(OnBlastKeyDownListener listener) {
        if (TextUtils.equals("F1F4", keyDownBuf) || TextUtils.equals("F4F1", keyDownBuf)) {
            listener.onBlast();
        } else {
            postDelayed(() -> {
                if (null != keyDownBuf)
                    keyDownBuf.delete(0, keyDownBuf.length());
            }, FAST_CLICK_INTERVAL);
        }
    }


    @Override
    public void checkBlastNettedKeyDown(int keyCode, OnBlastNettedKeyDownListener listener) {
        if (keyCode == KEY_F1) {
            listener.onCheckErrorCaps();
        } else if (keyCode == KEY_F2) {
            listener.onCharge();
        }
    }

    @Override
    public void checkSettingKeyDown(int keyCode, OnSettingKeyDownListener listener) {
        if (keyCode == KEY_MORE) {
            Timber.i("checkSettingKeyDown:" + keyCode + "," + mInputPsdStr);
            if (StringUtils.equals("1234", mInputPsdStr)) {
                listener.onMoreVisibility(true);
            } else if (StringUtils.equals("4321", mInputPsdStr)) {
                listener.onMoreVisibility(false);
            }
            mInputPsdStr = "";
        } else if (keyCode == KEY_1) {
            mInputPsdStr += "1";
        } else if (keyCode == KEY_2) {
            mInputPsdStr += "2";
        } else if (keyCode == KEY_3) {
            mInputPsdStr += "3";
        } else if (keyCode == KEY_4) {
            mInputPsdStr += "4";
        }
        if (keyCode == KEY_F1) {
            listener.onToggleModel();
        }
        if (keyCode == KEY_F2) {
            Timber.i("checkSettingKeyDown" + pressCount);
            pressCount++;
            if (pressCount >= 4) {
                listener.onLogOperate();
            }
            postDelayed(() -> {
                pressCount = 0;
            }, CLICK_INTERVAL);
        }
    }

    @Override
    public void checkUploadDataKeyDown(int keyCode, OnKeyDownListener listener) {
        if (keyCode == KEY_F1) {
            Timber.i("checkUploadDataKeyDown" + pressCount);
            pressCount++;
            if (pressCount >= 4) {
                listener.onSuccess();
            }
            postDelayed(() -> {
                pressCount = 0;
            }, CLICK_INTERVAL);
        }
    }

    @Override
    public void checkUploadDataDetailsKeyDown(int keyCode, OnKeyDownListener listener) {
        if (keyCode == KEY_F1) {
            Timber.i("checkUploadDataKeyDown" + pressCount);
            pressCount++;
            if (pressCount >= 4) {
                listener.onSuccess();
            }
            postDelayed(() -> {
                pressCount = 0;
            }, CLICK_INTERVAL);
        }
    }

    @Override
    public void aboutKeyDown(int keyCode, OnKeyDownListener listener) {
        if (keyCode == KEY_F1) {
            Timber.i("aboutKeyDown" + pressCount);
            pressCount++;
            if (pressCount >= 4) {
                listener.onSuccess();
            }
            postDelayed(() -> {
                pressCount = 0;
            }, CLICK_INTERVAL);
        }
    }

    private void checkKeyEvent(StringBuilder keyDownBuf, OnKeyDownListener listener) {
        Timber.i("checkKeyEvent:" + keyDownBuf);
        if (TextUtils.equals("F1F2", keyDownBuf) || TextUtils.equals("F2F1", keyDownBuf)) {
            listener.onSuccess();
        }
    }
}
